#include <QDebug>
#include <QPainter>
#include <QStyleOptionViewItem>
#include <QStyle>
#include <QFile>
#include "ThumbDelegate.h"

ThumbDelegate::ThumbDelegate(QObject *obj) : QItemDelegate(obj)
{
	m_teethName[18] = QStringList() << "" << "17";
	m_teethName[17] = QStringList() << "18" << "16";
	m_teethName[16] = QStringList() << "17" << "15";
	m_teethName[15] = QStringList() << "16" << "14";
	m_teethName[14] = QStringList() << "15" << "13";
	m_teethName[13] = QStringList() << "14" << "12";
	m_teethName[12] = QStringList() << "13" << "11";
	m_teethName[11] = QStringList() << "12" << "21";

	m_teethName[28] = QStringList() << "27" << "";
	m_teethName[27] = QStringList() << "26" << "28";
	m_teethName[26] = QStringList() << "25" << "27";
	m_teethName[25] = QStringList() << "24" << "26";
	m_teethName[24] = QStringList() << "23" << "25";
	m_teethName[23] = QStringList() << "22" << "24";
	m_teethName[22] = QStringList() << "21" << "23";
	m_teethName[21] = QStringList() << "11" << "22";

	m_teethName[38] = QStringList() << "37" << "";
	m_teethName[37] = QStringList() << "36" << "38";
	m_teethName[36] = QStringList() << "35" << "37";
	m_teethName[35] = QStringList() << "34" << "36";
	m_teethName[34] = QStringList() << "33" << "35";
	m_teethName[33] = QStringList() << "32" << "34";
	m_teethName[32] = QStringList() << "31" << "33";
	m_teethName[31] = QStringList() << "41" << "32";

	m_teethName[48] = QStringList() << "" << "47";
	m_teethName[47] = QStringList() << "48" << "46";
	m_teethName[46] = QStringList() << "47" << "45";
	m_teethName[45] = QStringList() << "46" << "44";
	m_teethName[44] = QStringList() << "45" << "43";
	m_teethName[43] = QStringList() << "44" << "42";
	m_teethName[42] = QStringList() << "43" << "41";
	m_teethName[41] = QStringList() << "42" << "31";
}

void ThumbDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	const QImage thumb = index.data(Qt::UserRole + 1).value<QImage>();
	const int teethIndex = index.data(Qt::UserRole + 2).toInt();

	if(thumb.isNull())
	{
		painter->save();
		painter->setPen(Qt::red);
		painter->drawText(option.rect, Qt::AlignCenter, tr("no file"));
		painter->restore();
	}
	else painter->drawImage(option.rect, thumb);

	QRect rectTextBG(option.rect);

	rectTextBG.setHeight(24);
	if(option.state & QStyle::State_MouseOver || option.state & QStyle::State_Selected)
	{
		painter->fillRect(rectTextBG, qRgb(60,162,255));
	}
	else painter->fillRect(rectTextBG, Qt::gray);

	QStringList list = m_teethName[teethIndex];
	QFont font = option.font;
	QRect rectText;
	{
		font.setPointSize(17);

		QFontMetrics fm(font);

		rectText = fm.boundingRect(QString::number(teethIndex));
		rectText.moveCenter(rectTextBG.center());
	}

	QRect rectPreText;
	{
		font.setPointSize(14);

		QFontMetrics fm(font);

		rectPreText = fm.boundingRect(list.at(0));
		rectPreText.moveBottomRight(rectText.bottomLeft());
		rectPreText.moveLeft(rectPreText.left() - 6);
	}

	QRect rectNextText;
	{
		font.setPointSize(14);

		QFontMetrics fm(font);

		rectNextText = fm.boundingRect(list.at(1));
		rectNextText.moveBottomLeft(rectText.bottomRight());
		rectNextText.moveLeft(rectNextText.left() + 6);
	}

	painter->save();
	painter->setPen(Qt::white);
	font.setPointSize(14);
	painter->setFont(font);
	painter->drawText(rectPreText, list.at(0));
	font.setPointSize(17);
	painter->setFont(font);
	painter->drawText(rectText, QString::number(teethIndex));
	font.setPointSize(14);
	painter->setFont(font);
	painter->drawText(rectNextText, list.at(1));

	painter->restore();
}
